document.body.style.backgroundColor = "skyblue";
let savelogurl = "http://39.105.231.80:9900/smscode/savelog/";
function delay(duration) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, duration);
  });
}

(async function () {
  await delay(1000);

  document.querySelector("span[title='外包管理']").click();

  await delay(1000);

  var event = new MouseEvent("mouseover", {
    view: window,
    bubbles: true,
    cancelable: false,
  });
  // 获取你想要触发鼠标移入事件的元素
  var element = document.querySelector("span[title='外包人员']");
  // 分发事件
  element.dispatchEvent(event);

  await delay(1000);

  document.querySelector("span[title='签到签出']").click();

  await delay(2000);

  //拿到第一行的元素
  let index = 1;
  let kqrq = document.querySelector(
    "tbody>tr:nth-child(" + index + ")>td[columnname='kqrq']>div"
  ).innerHTML;

  let qdsj = document.querySelector(
    "tbody>tr:nth-child(" + index + ")>td[columnname='qdsj']>div"
  ).innerHTML;

  let qcsj = document.querySelector(
    "tbody>tr:nth-child(" + index + ")>td[columnname='qcsj']>div"
  ).innerHTML;

  let qdbtn = document.querySelector(
    "tbody>tr:nth-child(" +
      index +
      ")>td[columnname='lbActionColumn']>div button:nth-child(1)"
  );

  let qcbtn = document.querySelector(
    "tbody>tr:nth-child(" +
      index +
      ")>td[columnname='lbActionColumn']>div button:nth-child(2)"
  );

  //判断当前时间是否在8点到8点半
  let toDay = new Date().getDate();
  let toHour = new Date().getHours();
  let toMinute = new Date().getMinutes();
  let isMorningClockTime = !!(toHour === 8 && toMinute > 10 && toMinute < 30);
  let isAfterClockTime = !!(toHour >= 17 && toHour <= 19);
  let needClockMornig = qdsj == "-" || !qdsj;
  let needClockAfter = qcsj == "-" || !qcsj;

  let qdrqDay = kqrq.substring(8);
  let istodayFlag = qdrqDay == toDay; //判断打卡的时间是不是今天,(排除节假日的情况)
  if (isMorningClockTime && istodayFlag) {
    if (needClockMornig) {
      qdbtn.click();
      setTimeout(_feedback, 3000);
    } else {
      //判断缓存里有没有值,没有的话,记录一次,有的话,不用了
      _saveClockCacheMorning(qdsj);
    }
  }
  if (isAfterClockTime && istodayFlag) {
    if (needClockAfter) {
      qcbtn.click();
      setTimeout(_feedback, 3000);
    } else {
      _saveClockCacheAfternoon(qcsj);
    }
  }

  function _saveClockCacheMorning(qdsj) {
    chrome.storage.local.get(
      `day${getNowFormatDate()}morning`,
      function (temp) {
        if (
          !temp[`day${getNowFormatDate()}morning`] ||
          temp[`day${getNowFormatDate()}morning`] == "-"
        ) {
          chrome.storage.local.set({
            [`day${getNowFormatDate()}morning`]: qdsj,
          });
        }
      }
    );
  }
  function _saveClockCacheAfternoon(qcsj) {
    chrome.storage.local.get(
      `day${getNowFormatDate()}afternoon`,
      function (temp) {
        if (
          !temp[`day${getNowFormatDate()}afternoon`] ||
          temp[`day${getNowFormatDate()}afternoon`] == "-"
        ) {
          chrome.storage.local.set({
            [`day${getNowFormatDate()}afternoon`]: qcsj,
          });
        }
      }
    );
  }
  function _feedback() {
    let kqrq = document.querySelector(
      "tbody>tr:nth-child(" + index + ")>td[columnname='kqrq']>div"
    ).innerHTML;

    let qdsj = document.querySelector(
      "tbody>tr:nth-child(" + index + ")>td[columnname='qdsj']>div"
    ).innerHTML;

    let qcsj = document.querySelector(
      "tbody>tr:nth-child(" + index + ")>td[columnname='qcsj']>div"
    ).innerHTML;
    let message = `kqrq${kqrq}qdsj${qdsj}qcsj${qcsj}`.replaceAll(" ", "");
    let finalUrl = savelogurl + message;
    _saveClockCacheAfternoon(qcsj);
    _saveClockCacheMorning(qdsj);
    fetch(finalUrl);
  }
  function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate = year + month + strDate;
    return currentdate;
  }
})();
